package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;


	/**
	 * 查询专辑下面声音列表
	 * @param albumId
	 * @param page
	 * @param limit
	 * @return
	 */
	@GuiguLogin(required = false)
	@Operation(summary = "获取专辑声音分页列表")
	@GetMapping("findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<IPage<AlbumTrackListVo>> findAlbumTrackPage(
			@PathVariable Long albumId,
			@PathVariable Long page,
			@PathVariable Long limit) {
		//用户id
		Long userId = AuthContextHolder.getUserId();
		Page<AlbumTrackListVo> pageParam = new Page(page,limit);
		IPage<AlbumTrackListVo> pageModel = trackInfoService.findAlbumTrackPage(pageParam,albumId,userId);
		return Result.ok(pageModel);
	}

	/**
	 * 修改声音
	 * @param trackId	声音ID
	 * @param trackInfoVo	请求体中声音的信息
	 * @return
	 */
	@PutMapping("updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
								  @RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.updateTrackInfo(trackId,trackInfoVo);
		return Result.ok();
	}


	/**
	 *根据id查询声音(用于修改声音的时候回显数据)
	 * @param trackId	声音ID
	 * @return
	 */
	@GetMapping("getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable("trackId") Long trackId) {
		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}

	/**
	 * 删除声音文件
	 * @param id 要删除的声音文件的id
	 * @return
	 */
	@DeleteMapping("removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id) {
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	/**
	 * 查询声音分类列表
	 * @param page	分页操作的当前页数
	 * @param limit	分页操作的每页的数据条数
	 * @param trackInfoQuery	请求体中传递的JSON格式的数据
	 * @return
	 */
	@GuiguLogin(required = true)
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping("findUserTrackPage/{page}/{limit}")
	public Result<IPage<TrackListVo>>
	findUserTrackPage(@PathVariable Long page,
					  @PathVariable Long limit,
					  @RequestBody TrackInfoQuery trackInfoQuery){
		//TODO 先将用户ID固定
		trackInfoQuery.setUserId(1L);
		//IPage<TrackListVo> 是 MyBatis-Plus 提供的分页接口，用于封装分页相关参数和结果
		//new Page<>(page, limit) 创建了一个 Page 对象
		Page<TrackListVo> pageParam = new Page<>(page, limit);
		IPage<TrackListVo> trackListVoIPage =
				trackInfoService.findUserTrackPage(pageParam,trackInfoQuery);
		// 返回数据
		return Result.ok(trackListVoIPage);
	}

	/**
	 * 保存声音
	 * @param trackInfoVo
	 * @return
	 */
	@Operation(summary = "新增声音")
	@PostMapping("saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	/**
	 * 上传声音
	 * @param file
	 * @return
	 */
	@Operation(summary = "上传声音")
	@PostMapping("uploadTrack")
	public Result<Map<String,Object>> uploadTrack(MultipartFile file) {

		// 调用服务层方法
		// 上传之后，腾讯云返回两个值，文件在腾讯云id 和 生成音频文件播放地址
		Map<String,Object> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}
}

